package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/album/")
@Api(tags = "03.相册管理模块")
@Slf4j
@Validated
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    // http://localhost:8080/album/add-new?name=TestName001&description=TestDescription&sort=99
    // @RequestMapping(value = "add-new",method = RequestMethod.POST)
    @PostMapping("add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam, @ApiIgnore HttpSession session) {
        log.debug("开始处理【添加相册请求，参数：{}】",albumAddNewParam);
        albumService.addNew(albumAddNewParam);
        return JsonResult.success();
    }

//    @PostMapping("delete")
//    @ApiOperation("根据id删除相册")
//    @ApiOperationSupport(order = 200)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, dataType = "long"),
//            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "long")
//    })
//    public String delete(@Range(min = 1, message = "相册ID不合法！")
//                         @RequestParam Long albumId, Long userId) {
//        return null;
//    }

    @PostMapping("delete")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "相册ID不合法！")
                         @RequestParam Long albumId) {
        albumService.deleteById(albumId);
        return JsonResult.success();
    }

    @PostMapping("updateInfoById")
    @ApiOperation("更改相册详情信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的相册id") Long id,
                                     @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        albumService.updateInfoById(id,albumUpdateInfoParam);
        return JsonResult.success();
    }

    @GetMapping("list")
    @ApiOperation("分页查询相册列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int")
    public JsonResult list(@RequestParam @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值！") Integer pageNum) {
        return JsonResult.success(albumService.list(pageNum));
    }

    @GetMapping("listAll")
    @ApiOperation("查询所有相册列表")
    @ApiOperationSupport(order = 401)
    public JsonResult listAll() {
        Integer pageNum = 1;
        Integer pageSize = Integer.MAX_VALUE;
        return JsonResult.success(albumService.list(pageNum, pageSize));
    }

    @GetMapping("getStandardById")
    @ApiOperation("通过id查询一条数据")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@RequestParam @Range(min = 1, message = "根据ID查询相册详情失败，请提供正确的ID值！") Long id) {
        return JsonResult.success(albumService.getStandardById(id));
    }
}
